<div class="row">
    <div class="col-lg-12">
        <h3 class="page-header">Sửa chi tiết đơn hàng</h3>
    </div>
    <!-- /.col-lg-12 -->
</div>
<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-default">
            <div class="panel-body">
                <div class="row">
                    <div class="col-lg-12">
                        <form role="form" method="POST" name="AddOrder">
                            <div class="form-group">
                                <div class="row">   
                                    <div class="col-lg-2">
                                        <button type="button" class="btn btn-default" onclick="window.location = '<?php echo $this->baseUrl()."/kinhdoanh/order/detail/id/".$this->item->id ?>'" >Quay lại</button>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                    <div class="row">
                                        <div class="col-lg-2">
                                            Mã đơn hàng
                                        </div>
                                        <div class="col-lg-3">
                                            <input name="order_no" type="text" class="form-control" value="<?php echo $this->item->order_no ?>" readonly="true">
                                        </div>
                                        <div class="col-lg-2">
                                            Tên cửa hàng
                                        </div>
                                        <div class="col-lg-3">
                                            <input type="text" class="form-control" value="<?php echo GlobalLib::getName("agent",$this->item->store_id,'name') ?>" readonly="true">
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="row">
                                       <div class="col-lg-2">
                                            Ngày tạo
                                        </div>
                                        <div class="col-lg-3">
                                            <input type="text" class="form-control" value="<?php echo GlobalLib::viewDate($this->item->created)?>"readonly="true">
                                        </div>
                                        <div class="col-lg-2">
                                            Tổng điểm
                                        </div>
                                        <div class="col-lg-3">
                                            <input type="text" class="form-control" value="<?php echo $this->item->total_charges ?>"readonly="true">
                                        </div>
                                    </div>
                                </div>
                            <div class="form-group">
                                <div class="row">                                    
                                    <div class="col-lg-2">
                                        <label>DS Đại lý</label>
                                    </div>
                                     <div class="col-lg-3">
                                        <?php echo $this->agentHTML; ?>
                                    </div>                       
                                    <div class="col-lg-2">
                                        <label>Ngày mua hàng</label>
                                    </div>
                                    <div class="col-lg-3">
                                        <input class="form-control" type="datetime" name="order_created" id="order_created" value="<?php echo GlobalLib::viewDate($this->item_detail->order_created)  ?>" >
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="row">
                                    
                                    <div class="col-lg-2">
                                        <label>DS Sản phẩm</label>
                                    </div>
                                      <div class="col-lg-3">
                                        <?php echo $this->productHTML ?>
                                    </div>     
                                     <div class="col-lg-2">
                                        <label>Số lượng</label>
                                    </div>
                                    <div class="col-lg-3" id="quantity_id">
                                        <input id="quantity" class="form-control" name="quantity" onkeydown="change_text();" onkeyup="change_text();" value="<?php echo $this->item_detail->quantity ?>">
                                    </div>
                                </div>
                            </div>
                             <div class="form-group">
                                <div class="row">                                     
                                     
                                    <div class="col-lg-2">
                                        <label>Hệ số điểm</label>
                                    </div>
                                    <div class="col-lg-3" id="factor_id">
                                        <input id="factor" class="form-control" name="factor" onkeydown="change_text();" onkeyup="change_text();" readonly="true" value="<?php echo $this->item_detail->factor ?>">
                                    </div>        
                                    <div class="col-lg-2">
                                        <label>Điểm nhận được</label>
                                    </div>
                                      <div class="col-lg-3" id="charges_id">
                                          <input id="charges" class="form-control" name="charges" readonly="true" value="<?php echo $this->item_detail->charges ?>"> 
                                      </div> 
                                </div>
                            </div>                               
                            <div class="form-group">
                                <input type="hidden" name="submit_type" id="submit_type" value="2">
                                <button onclick="return add(2);" type="submit" class="btn btn-primary">Duyệt</button>
                                <button onclick="return add(1);" type="submit" class="btn btn-primary">Lưu</button>
                                <button type="reset" class="btn btn-default">Đặt lại</button>                                
                                <button onclick="return showModal(2);" type="button" class="btn btn-default">Thêm đại lý mới</button>
                            </div>          
                            <div class="form-group">
                           <div class="col-lg-12">
                                <div class="table-reponsive">
                                    <div id="jqxgrid">                        
                                    </div>
                                </div>
                            </div>                
                            </div>                            
                            <script type="text/javascript">                                
                                document.forms['AddOrder'].name.focus();
                                function set_focus() {
                                    document.forms['AddOrder'].name.focus();
                                }
                                function add(submit_type){                                    
                                    if($("#store_id").val()==$("#agent_id").val())
                                    {
                                        bootbox.alert("Vui lòng chọn cửa hàng và đại lý khác nhau!");
                                        return false;
                                    }
                                    if($("#store_id").val()=="0")
                                    {
                                        bootbox.alert("Vui lòng chọn cửa hàng");
                                        return false;
                                    }
                                    if($("#agent_id").val()=="0")
                                    {
                                        bootbox.alert("Vui lòng chọn đại lý");
                                        return false;
                                    }
                                    if($("#product_id").val()=="0")
                                    {
                                        bootbox.alert("Vui lòng chọn sản phẩm");
                                        return false;
                                    }
                                    if($("#factor").val()=="")
                                    {
                                        bootbox.alert("Sản phẩm này chưa được thiết lập hệ số điểm");
                                        return false;
                                    }
                                    if($("#quantity").val()=="" || isNaN($("#quantity").val())==true)
                                    {
                                        bootbox.alert("Vui lòng nhập số lượng");
                                        return false;
                                    }                 
                                    $("#submit_type").val(submit_type);                                                                                                         
                                };
                                </script>                                
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>   
</div>
<script type="text/javascript">
        $(document).ready(function () {
             $("#quantity").keydown(function (e) {
                    // Allow: backspace, delete, tab, escape, enter and .
                    if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
                         // Allow: Ctrl+A
                        (e.keyCode == 65 && e.ctrlKey === true) || 
                         // Allow: home, end, left, right
                        (e.keyCode >= 35 && e.keyCode <= 39)) {
                             // let it happen, don't do anything
                             return;
                    }
                    // Ensure that it is a number and stop the keypress
                    if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
                        e.preventDefault();
                    }
                });
            // prepare the data
            var source =
            {
                datatype: "json",
                datafields:
                [
                    { name: 'id',type:'int'},
                    { name: 'product_name', type: 'string' },     
                    { name: 'quantity', type: 'string' },
                    { name:'price',type:'float'},
                    { name:'factor',type:'float'},
                    { name:'charges',type:'float'},
                    { name: 'order_created', type: 'string'},
                    { name:'agent_name',type:'string'},                    
                    {name: 'order_status_name', type:'string'},
                    {name: 'order_status_id', type:'int'}
                ],
                url: "<?php echo $this->baseUrl().'/kinhdoanh/order/servicedetail/order_id/'.$this->item->order_no ?>",
                sortcolumn: 'order_area',
                sortdirection: 'asc'
            };
         var dataAdapter = new $.jqx.dataAdapter(source, {
                loadComplete: function () {
                }
            });
            $("#jqxgrid").jqxDataTable(
            {
                source: dataAdapter,
                theme:'bootstrap',
                pageable: true,
                altRows: true,
                sortable: false,                
                width: '100%',                
                ready: function () {
                    $("#jqxgrid").jqxDataTable('hideColumn', 'id');
                },
                columns: [ 
                  { text: 'id',hidden:true,dataField:'id',cellsalgin:'right',width:0},
                  { text: 'in',hidden:true,dataField:'in',cellsalgin:'right',width:0},
                  { text: 'order_status_id',hidden:true,dataField:'order_status_id',cellsalgin:'right',width:0},
                  { text: 'Tên sản phẩm',hidden:false,dataField:'product_name',cellsalgin:'right',width:'25%'},
                  { text: 'Ngày mua',hidden:false,editable: false, dataField: 'order_created', width: '10%' },
                  { text: 'Tên đại lý',hidden:false,editable: false, dataField: 'agent_name', width: '20%' },
                  { text: 'Số lượng',hidden:false,dataField:'quantity',cellsalgin:'right',width:'10%'},                                                      
                  { text: 'Hệ số', editable: false, dataField: 'factor', width:'10%' },
                  { text: 'Số điểm nhận', editable: false, dataField: 'charges', width:'10%' },
                  { text: 'Trạng thái', editable: false, dataField: 'order_status_name', width:'15%' }                
                ]
            });
           
        });
    </script>
<form  id="addAgent" method="POST" onsubmit="if(document.getElementById('AgentorStore').innerHTML == 'Thêm đại lý mới') return checkAgent(2); else return checkAgent(1);">
    <div id="myModal" class="modal fade">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                        &times;</button>
                    <h4 class="modal-title">
                        <center>
                            <div id="AgentorStore">                                
                            </div>                            
                        </center>
                    </h4>
                </div>
                <div class="modal-body">    
                    
                    <div class="row">
                        <div class="col-lg-3">                            
                            Tên
                        </div>
                        <div class="col-lg-9">
                            <input class="form-control"  id="agent_name" name="oldpassword" /></div>
                    </div>
                    
                    <div class="row">
                        <div class="col-lg-3">
                            Địa chỉ</div>
                        <div class="col-lg-9">
                            <input class="form-control"  id="agent_address" name="password" /></div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3">
                            Khu vực</div>
                        <div class="col-lg-9">
                            <?php echo $this->popupareaHTML ?>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3">
                            Tỉnh thành</div>
                        <div class="col-lg-9">
                            <?php echo $this->popupprovinceHTML ?>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3">
                            Quận huyện</div>
                        <div class="col-lg-9">
                            <?php echo $this->popupdistrictHTML ?>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3">
                            Người đại diện</div>
                        <div class="col-lg-9">
                            <input class="form-control"  id="representavie" name="repassword" /></div>                       
                    </div>
                    <div class="row">
                        <div class="col-lg-3">
                            Số điện thoại</div>
                        <div class="col-lg-9">
                            <input class="form-control"  id="phone" name="repassword" /></div>                       
                    </div>
                    <div class="row">
                        <div class="col-lg-3">
                            Loại đại lý</div>
                        <div class="col-lg-9">
                            <?php echo GlobalLib::getCombo('agent_cat_id', 'agent_category', 'id', 'name', 0,false,'',null,'class="form-control"') ?> 
                        <div class="col-lg-2">
                            <span style="color: Red;"></span></div>
                    </div>
                </div>                    
                <div class="modal-footer">    
                    <center>
                        <button  class="btn btn-primary" type="submit">
                            Thêm
                        </button>
                        <button type="button" id="close" class="btn btn-default" data-dismiss="modal">
                            Đóng
                        </button>
                    </center>
                </div>
            </div>
        </div>
    </div>
</form>
<script type="text/javascript">
$(document).ready(function(){
            $("#order_created").datepicker({
                dateFormat: "dd-mm-yy",
                onSelect: function() {
                                   var params = {
                    action:'getproduct',
                    agent_id: $('#agent_id').val(),
                    product_id:$('#product_id').val(),
                    order_created:$('#order_created').val()
                };
                $.post('<?php echo $this->baseUrl() ?>/kinhdoanh/service/index', params,
                            function (data) {
                                 var items = eval(data);
                                var jItem = items[0];
                               $('#product_id').html(jItem.html);
                               $('#charges_id').html(jItem.html3);
                               $('#factor_id').html(jItem.html1);
                               $('#quantity_id').html(jItem.html2);
                            });
            }
            })
        });
function change_text()
{
    var quantity= $('#quantity').val();
    var factor=$('#factor').val();
    var charges=quantity*factor;
    document.getElementById('charges').value=charges;
}
function showModal(cat)
{
    if(cat=="2") document.getElementById("AgentorStore").innerHTML = "Thêm đại lý mới";
    else if(cat=="1") document.getElementById("AgentorStore").innerHTML = "Thêm cửa hàng mới";
    $('#myModal').modal('show');
};
function checkAgent(cat)
{        
    if($("#agent_name").val()==="") {bootbox.alert("Vui lòng nhập tên của đại lý"); return false;};
    var paramss =
    {
        name: $("#agent_name").val(),
        address: $("#agent_address").val() ,
        area_id: $("#popup_area_id").val(),
        province_id: $("#popup_province_id").val(),
        district_id: $("#popup_district_id").val(),
        representavie: $("#representavie").val(),
        phone: $("#phone").val(),
        agent_cat_id: $("#agent_cat_id").val()
    };
    
    $.post('<?php echo $this->baseUrl()."/kinhdoanh/order/addagent" ?>',paramss,function(data){        
        $('#myModal').modal('hide');
        if(cat=="2")            
            $("#agent_id").append("<option value='"+data+"' selected='selected'>"+$("#agent_name").val()+"</option>");
        else if(cat=="1") $("#store_id").append("<option value='"+data+"' selected='selected'>"+$("#agent_name").val()+"</option>");
    });
    return false;
};
</script>


                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        